package com.zjpavt.client.util;


import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

/**
 * 读取Properties文件 工具类
 *
 * @author gxy
 */
public class PropertiesUtil {
    private static final Logger log = LoggerFactory.getLogger(PropertiesUtil.class);
    private static final HashMap<String, Properties> propertiesHashMap = new HashMap<>();

    /**
     * 用于对Properties文件的缓存
     * @param configFileName Properties文件名称
     */
    private static Properties getProp(String configFileName) throws IOException {
        synchronized (propertiesHashMap) {
            if (propertiesHashMap.containsKey(configFileName)) {
                return propertiesHashMap.get(configFileName);
            }
            Properties prop = new Properties();
            InputStream im = null;
            try {
                File configDir = new File(ClientConfig.CONFIG_PATH);
                File configFile = new File(ClientConfig.CONFIG_PATH + configFileName);
                if (!configDir.exists()) {
                    configDir.mkdirs();
                }
                if (!configFile.exists()) {
                    configFile.createNewFile();
                }
                im = new FileInputStream(configFile);
                prop.load(im);
                propertiesHashMap.put(configFileName, prop);
            }
            finally {
                IOUtils.closeQuietly(im);
            }
            return prop;
        }
    }


    /**
     * 获取配置文件键值
     *
     * @param configFileName Properties文件名称
     * @param key 文件里的key
     * @return key对应的value
     */
    public static String getProperties(String configFileName, String key) throws IOException {
        return getProp(configFileName).getProperty(key);
    }
    public static String getProperties(String configFileName, String key,String defaultValue) throws IOException {
        return getProp(configFileName).getProperty(key,defaultValue);
    }
    public static Set<String> getPropertiesKeySet(String configFileName) {
        try {
            return getProp(configFileName).stringPropertyNames();
        } catch (IOException e) {
            log.error(LoggerUtil.convertException2String(e));
        }
        return new HashSet<>();
    }
    /**
     * 获取配置文件键值
     *	clientStatus.properties
     * @param key 文件里的key
     * @return 返回默认Properties的key对应的value
     */
    public static String getPropertiesDefault(String key) throws IOException {
        return getProperties(ClientConfig.DEVICE_STATUS_CONFIG_NAME, key);
    }

    /**
     * 获取配置文件键值
     *	clientStatus.properties
     * @param key 文件里的key
     * @return 返回默认Properties的key对应的value
     */
    public static String getPropertiesDefault(String key,String defaultValue) throws IOException {
        return getProperties(ClientConfig.DEVICE_STATUS_CONFIG_NAME, key,defaultValue);
    }
    /**
     * 修改文件的键值对
     * @param configFileName Properties文件名称
     * @param key 文件里的key
     * @param value 新的value
     * @throws IOException
     */
    public static synchronized void setPropertyOrReplace(String configFileName, String key, String value) throws IOException {
//		缓存与获取一直的时候不需要切换
        if (StringUtil.equals(value,getProperties(configFileName,key))) {
            return;
        }
        Properties properties = getProp(configFileName);
        OutputStream out = null;
        try {
            out = new FileOutputStream(ClientConfig.CONFIG_PATH
                                       + configFileName);
            if (properties.containsKey(key)) {
                properties.replace(key, value);
            } else {
                properties.setProperty(key, value);
            }
            properties.store(out, null);
        }
        finally {
            if (out != null) {
                out.close();
            }
        }
        try {
            ProcessUtil.runCommandWait("sync");
        } catch (InterruptedException e) {
            log.error(LoggerUtil.convertException2String(e));
        }
    }

    /**
     * 修改默认文件的键值对
     *clientStatus.properties
     * @param key 文件里的key
     * @param value 新的value
     * @throws IOException
     */
    public static void setPropertyOrReplaceDefault(String key, String value) throws IOException {
        setPropertyOrReplace(ClientConfig.DEVICE_STATUS_CONFIG_NAME, key, value);
    }

    public static Properties getConfigProperties(String propertiesFilePath) throws IOException{
        Properties p=new Properties();
        InputStream im=null;
        try {
            File file2 = FileUtil.makeSureFileExist(propertiesFilePath);
            im=new FileInputStream(file2);
            p.load(im);
            return p;
        }
        finally{
            if(im!=null) {
                im.close();
            }
        }
    }
}
